
import { tt } from "../../ttapi_interface/ttapi.js";
import * as tt2 from "../../ttapi_layer2/ttlayer2.js";
import { EUI_Style } from "./EUI_Style.js";
import { EUI_Window } from "./EUI_Window.js";

export interface EUI_IMenuShower {
    ShowContextMenu(item: EUI_MenuItemData, rect: tt.Rectangle): void
    PopAllMenu(): void
}
export class EUI_MenuItemData {
    constructor(bar: EUI_IMenuShower, title: string) {
        this.title = title;
        this.bar = bar;
    }
    private bar: EUI_IMenuShower
    OnClick(rect: tt.Rectangle): void {
        console.log("Menu Click:" + this.title);
        if (this.items != null && this.items.length > 0) {
            this.bar.ShowContextMenu(this, rect);
        }
        else {
            this.bar.PopAllMenu();
            if (this.action != null)
                this.action();
        }
    }
    AddMenu(names: string[], action: (() => void) | null): EUI_MenuItemData {
        let firstname = names[0];
        let nextnames: string[] = [];
        if (names.length > 1) {

            for (var i = 1; i < names.length; i++) {
                nextnames.push(names[i]);
            }
        }
        let fmenu: EUI_MenuItemData = null;
        for (var i = 0; i < this.items.length; i++) {
            if (this.items[i].title == firstname) {
                fmenu = this.items[i];
            }
        }

        //"_"开头的是分割线
        if (names.length == 1 && firstname.indexOf("_") == 0) {
            fmenu = null;
        }

        if (fmenu == null) {
            fmenu = new EUI_MenuItemData(this.bar, firstname);
            this.items.push(fmenu);
        }
        if (nextnames.length > 0) {
            return fmenu.AddMenu(nextnames, action);
        }
        else {
            fmenu.action = action;
            return fmenu;
        }
    }
    title: string;
    items: EUI_MenuItemData[] = [];
    action: () => void;
}

export class EUI_MenuBar extends tt2.QUI_Panel implements EUI_IMenuShower {
    public constructor(win: EUI_Window, font: tt.Font, parent: tt2.QUI_IElement) {
        super()
        this.win = win;
        this.font = font;

        this.localRect.setHPosFill(0, 0);
        this.localRect.setVPosByTopBorder(16, 0);

        this._x = 0;
        this.menuItems = []
    }
    win: EUI_Window
    font: tt.Font;

    private _x: number
    menuItems: EUI_MenuItemData[];

    RefreshMenu(): void {
        this.container.removeChildAll();
        this._x = 0;
        for (var i = 0; i < this.menuItems.length; i++) {
            this._AddMenuBtn(this.menuItems[i]);
        }
    }
    private _AddMenuBtn(item: EUI_MenuItemData) {

        let btn = new tt2.QUI_Button();
        btn.OnClick = () => {
            item.OnClick(btn.getWorldRect());
        }
        let normal = EUI_Style.CreateWhiteImage();
        normal.localRect.setAsFill();

        let labelnor = new tt2.QUI_Label(this.font, item.title);
        labelnor.localRect.setAsFill();
        labelnor.color = new tt.Color(0.2, 0.2, 0.2, 1.0);
        //labelnor.fontScale = new tt.Vector2(2, 2);
        normal.addChild(labelnor);

        btn.ElemNormal = normal;


        let press = EUI_Style.CreateWhiteImage();
        press.localRect.setAsFill();
        press.color = new tt.Color(0, 0, 0, 1.0);

        let labelp = new tt2.QUI_Label(this.font, item.title);
        labelp.localRect.setAsFill();
        labelp.color = new tt.Color(0.8, 0.8, 0.8, 1.0);
        //labelp.fontScale = new tt.Vector2(2, 2);
        press.addChild(labelp);


        btn.ElemPress = press;
        let size = this.font.SureText(item.title, labelp.fontScale.X);
        btn.localRect.setByRect(new tt.Rectangle(this._x, 0, size + 32, 14));

        this.addChild(btn);
        this._x += (size + 40)
    }

    AddMenuItem(name: string, action: (() => void) | null = null): EUI_MenuItemData {
        let names = name.split('/');
        let firstname = names[0];
        let nextnames: string[] = [];
        if (names.length > 1) {

            for (var i = 1; i < names.length; i++) {
                nextnames.push(names[i]);
            }
        }

        let fmenu: EUI_MenuItemData = null;
        for (var i = 0; i < this.menuItems.length; i++) {
            if (this.menuItems[i].title == firstname) {
                fmenu = this.menuItems[i];
            }
        }

        //"_"开头的是分割线
        if (names.length == 1 && firstname.indexOf("_") == 0) {
            fmenu = null;
        }
        if (fmenu == null) {

            fmenu = new EUI_MenuItemData(this, firstname);
            this.menuItems.push(fmenu);
        }
        if (nextnames.length > 0) {
            return fmenu.AddMenu(nextnames, action);
        }
        else {
            fmenu.action = action;
            return fmenu;
        }
    }
    ShowContextMenu(item: EUI_MenuItemData, rect: tt.Rectangle) {
        let cmenu = new EUI_ContextMenu(this.win, this.font);

        cmenu.backElement = tt2.QUI_Clone(this.backElement);
        cmenu.borderElement = tt2.QUI_Clone(this.borderElement);
        cmenu.localRect.setByRect(new tt.Rectangle(rect.X + rect.Width, rect.Y + rect.Height, 250, 150));
        cmenu.UpdateItem(item);
        this.win.PushModel(cmenu, "_menu_", () => {
            this.PopAllMenu();
        });
    }
    PopAllMenu(): void {
        while (true) {
            let tag = this.win.GetPopTag();
            if (tag != "_menu_")
                break;
            this.win.PopModel();
        }
    }
}
export class EUI_ContextMenu extends tt2.QUI_Panel implements EUI_IMenuShower {
    constructor(win: EUI_Window, font: tt.Font) {
        super();
        this.win = win;
        this.font = font;
    }

    win: EUI_Window;
    font: tt.Font
    UpdateItem(item: EUI_MenuItemData) {
        this.container.removeChildAll();
        this._y = 0;
        for (var i = 0; i < item.items.length; i++) {
            this._AddMenuBtn(item.items[i]);
        }
        this.localRect.offsetY2 = this.localRect.offsetY1 + this._y + 2;
    }

    private _AddMenuBtn(item: EUI_MenuItemData) {

        if (item.title.indexOf("_") == 0) {
            let label = EUI_Style.CreateLabel("____");
            label.halign = tt2.QUI_HAlign.Left;
            label.localRect.setHPosFill();
            label.localRect.setVPosByTopBorder(14, this._y);
            this.addChild(label);
        }
        else {
            let btn = new tt2.QUI_Button();
            btn.OnClick = () => {
                item.OnClick(btn.getWorldRect());
            }
            let normal = EUI_Style.CreateWhiteImage();
            normal.localRect.setAsFill();

            let labelnor = new tt2.QUI_Label(this.font, item.title);
            labelnor.localRect.setAsFill();
            labelnor.color = new tt.Color(0.2, 0.2, 0.2, 1.0);
            //labelnor.fontScale = new tt.Vector2(2, 2);
            normal.addChild(labelnor);

            btn.ElemNormal = normal;


            let press = EUI_Style.CreateWhiteImage();
            press.localRect.setAsFill();
            press.color = new tt.Color(0, 0, 0, 1.0);

            let labelp = new tt2.QUI_Label(this.font, item.title);
            labelp.localRect.setAsFill();
            labelp.color = new tt.Color(0.8, 0.8, 0.8, 1.0);
            //labelp.fontScale = new tt.Vector2(2, 2);
            press.addChild(labelp);


            btn.ElemPress = press;
            let size = this.font.SureText(item.title, labelp.fontScale.X);
            btn.localRect.setByRect(new tt.Rectangle(0, this._y, size + 32, 14));

            this.addChild(btn);
        }
        this._y += (16)
    }
    private _y: number = 0;


    ShowContextMenu(item: EUI_MenuItemData, rect: tt.Rectangle) {
        let cmenu = new EUI_ContextMenu(this.win, this.font);

        cmenu.backElement = tt2.QUI_Clone(this.backElement);
        cmenu.borderElement = tt2.QUI_Clone(this.borderElement);
        cmenu.localRect.setByRect(new tt.Rectangle(rect.X + rect.Width, rect.Y + rect.Height, 400, 300));
        cmenu.UpdateItem(item);
        this.win.PushModel(cmenu, "_menu_", () => {
            this.PopAllMenu();
        });
    }
    PopAllMenu(): void {
        while (true) {
            let tag = this.win.GetPopTag();
            if (tag != "_menu_")
                break;
            this.win.PopModel();
        }
    }
}